package com.lxc.mapping;

import javax.servlet.http.*;
import com.lxc.service.MusicDatabase;
import com.lxc.common.Music;
import java.io.*;
public class GetMusic{
    HttpServletRequest request;
    HttpServletResponse response;
    public GetMusic(HttpServletRequest request,HttpServletResponse response){
        this.request = request;
        this.response = response;
    }
    public void getMusic(){
        //从请求头中获取相应的参数值
		String idString = request.getParameter("id");
		int id = Integer.parseInt(idString);
		MusicDatabase musicDatabase = new MusicDatabase();
		Music music = musicDatabase.getMusic(id);
		String musicAddress = music.getMusicAddress();
		try{
		OutputStream out = response.getOutputStream();
		FileInputStream in = new FileInputStream(musicAddress);
		byte[] musicBytes = new byte[in.available()];
		in.read(musicBytes);
		out.write(musicBytes);
		}
		catch(IOException ex){
			System.out.println("读取音频失败");
		}
        //System.out.println(""+id);
		//System.out.println("mapping,getMusic:" + music);
    }
}